$(".loading").show();
var currentPage = 1;
var pageSize = 10;
var totalPage = 1;
var access_token = '';
var isIOS = false;
var _h3 = $(".wrap").height();


function load_list() {
	$(".loading").show();
	$.ajax({
		url: $.httpRootPath() + "/information/feedbackList?access_token=" + access_token,
		// url: "http://10.173.232.154:8012/appServer/information/feedbackList?access_token=" + access_token,
		type: "GET",
		data: {
			"currentPage": currentPage,
			"pageSize": pageSize,
			"random": Math.random()
		},
		dataType: 'json',
		async: false, //同步
		timeout: 10000,
		success: function(data) {

			var data = JSON.parse(data)
			var str = data.object;

			totalPage = data.pager.totalPage;
			currentPage = data.pager.currentPage;

			var html = '';

			if (str.length == 0) {
				$(".no_data").show();
			} else {

				if (currentPage != 1) {
					html = '<div class="page page_prev">下拉加载上一页</div>';
				}

				for (var i = 0; i < str.length; i++) {

					html += '<div class="box">';
					if(!str[i].headURL){
						str[i].headURL = 'website/img/yonghuxinxi_pic_logo.png';
					}
					html += '<i class="userheader"><img src="' + str[i].headURL + '" alt="user header" /></i><p class="box_text">';
					html += str[i].content + ' &nbsp;</p><p class="box_img">';
					if (str[i].pic1) {
						html += '<span><img src="' + str[i].pic1 + '" alt="" /></span>';
					}
					if (str[i].pic2) {
						html += '<span><img src="' + str[i].pic2 + '" alt="" /></span>';
					}
					if (str[i].pic3) {
						html += '<span><img src="' + str[i].pic3 + '" alt="" /></span>';
					}

					html += '</p><p class="box_time">' + str[i].feedBackTempTime + '</p>';

					if (str[i].reply) {
						html += '<div class="reply"><span>系统回复<i class="down"></i></span></div>';
						html += '<div class="slide_mobile"><div class="reply_text"><p class="box_text">' + str[i].reply + '</p>';
						html += '<p class="box_time">' + str[i].operateTempTime + '</p></div></div>';
					}

					html += '</div>';
				}

				if (currentPage != totalPage) {
					html += '<div class="page page_next">上拉加载下一页</div>'
				} else {
					html += '<div class="page page_next">没有更多记录了</div>';
				}
			}


			$(".wrap").html(html);
			setTimeout(function() {
				$(".loading").hide();
			}, 500);


			$(".wrap").height("auto");
			_h3 = $(".wrap").height();
			$(".wrap").height("100%");

			$(".history .reply").on("touchstart", function(event) {
				$(this).find('i').toggleClass('up');
				$(this).next(".slide_mobile").stop().slideToggle();
			})

		},
		error: function(data) {
			console.log(data);
			$(".no_data").show();
			setTimeout(function() {
				$(".loading").hide();
			}, 500);
		}
	});
}



function say() {
	window.webkit.messageHandlers.setDeviceIos.postMessage({
		body: 'hello world!'
	});
}

function setIOS() {
	isIOS = true;
}

if (isIOS) {
	function IOS_getAccessToken(token) {
		access_token = token;
		load_list();
	}
} else {
	try {
		access_token = window.callJava.getAccessToken();
		load_list();
	} catch (e) {
		// console.log(e)
		// access_token = 'b5f7853d-e48d-4c53-bbb1-09510f786bc0';
		// // access_token = '9fdae309-3e9b-471f-9edc-4ba16418ed2c';
		// load_list();
	}
}


///----------页面交互-------
var starty, //触碰时的y坐标
	endy, //离开时的y坐标
	endy_m; //移动时候的坐标
var e_scrollTop = $(".wrap").scrollTop();
var time_start, time_end, speed;

$(".wrap").on("touchstart", function(event) {
	var elem = $(this);
	var event = event || window.event;
	event.preventDefault(); //用于修复在移动(Android 4.0.4 手机)浏览器上测试后发现 touchend 事件没有如约触发的bug。
	starty = event.originalEvent.targetTouches[0].pageY;
	e_scrollTop = elem.scrollTop();
	time_start = (new Date()).valueOf();
});

$(".wrap").on("touchmove", function(event) {
	var elem = $(this);
	var event = event || window.event;
	endy_m = event.originalEvent.targetTouches[0].pageY;
	elem.scrollTop(e_scrollTop + starty - endy_m);
});

$(".wrap").on("touchend", function(event) {

	var elem = $(this);
	event.stopPropagation();

	var event = event || window.event;
	endy = event.originalEvent.changedTouches[0].pageY;
	var ele_top = elem.scrollTop(); //elem的可视区域是否发生滚动	
	var _h1 = elem.height();
	var _h2 = elem.scrollTop();

	time_end = (new Date()).valueOf();
	e_scrollTop = elem.scrollTop();

	if (time_end > time_start) {
		var difference_time = time_end - time_start;
		var difference_y = Math.abs(endy - starty);
		speed = difference_y / difference_time;

	}

	if (endy < starty) {
		if (_h1 + _h2 == _h3 && currentPage < totalPage) {
			currentPage += 1;
			load_list();
			elem.scrollTop(28);
		} else if (speed > 1) {
			elem.animate({
				scrollTop: e_scrollTop + 1000
			}, 300);
		}

	} else if (endy > starty) {
		if (_h2 == 0 && currentPage != 1) { //elem的可视区域有滚动的情况下不触发滑动效果                        
			console.log("加载上一页");
			currentPage -= 1;
			load_list();
			if (currentPage == 1) {
				elem.scrollTop(0);
			} else {
				elem.scrollTop(28);
			}
		} else if (speed > 1) {
			elem.animate({
				scrollTop: e_scrollTop - 1000
			}, 300);
		}
	}

});